<template>
  <div class="page">
    <div className="container">
      <div className="login-card">
        <div className="logo">
          <h1>{{ config.projectName }} - 服务商签约</h1>
          <p>请输入手机号获取验证码</p>
        </div>

        <form id="loginForm">
          <div className="input-group">
            <label htmlFor="phone">手机号码</label>
            <div className="input-with-button">
              <input
                class="input"
                type="tel"
                id="phone"
                placeholder="请输入手机号码"
                required
                v-model="phone"
              />
              <div class="button" @click="getCode">
                <span>获取验证码</span>
                <span v-if="isTiming" style="margin-left: 5px"
                  >({{ second }}s)</span
                >
              </div>
            </div>
          </div>

          <div className="input-group">
            <label htmlFor="code">验证码</label>
            <div style="display: flex">
              <input
                class="input-field"
                type="text"
                id="code"
                placeholder="请输入验证码"
                required
                v-model="code"
              />
            </div>
          </div>

          <div @click="submit" className="login-button">登录</div>
        </form>

        <div className="divider">
          <span>其他登录方式</span>
        </div>
        <div className="footer">
          <p>
            登录即表示同意<a
              href="https://app.diaochan88.com/h5/bundle/pages/web/web?title=service"
              >用户协议</a
            >和<a
              href="https://app.diaochan88.com/h5/bundle/pages/web/web?title=privacy"
              >隐私政策</a
            >
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { config } from "../../common/config";
import { request } from "../../common/api";

const second = ref(60);
const isTiming = ref(false);
const phone = ref("");
const code = ref("");

const submit = () => {
  if (!phone.value) {
    uni.showToast({
      title: "请填写手机号码",
      icon: "error",
    });
    return;
  }
  if (!code.value) {
    uni.showToast({
      title: "请填写验证码",
      icon: "error",
    });
    return;
  }
  uni.showLoading({
    title: "登录中...",
    mask: true,
  });
  request({
    url: "login/login2",
    method: "POST",
    data: {
      username: phone.value,
      sms_code: code.value,
      type: 2,
      needtechid: 1,
    },
  })
    .then((res) => {
      if (res.code === 1) {
        uni.setStorageSync("token", res.data.token);
        uni.setStorageSync("tech_id", res.data.tech_id);
        uni.navigateTo({
          url: "/pages/contract/index",
        });
      } else {
        uni.showToast({
          title: res.msg || "登录失败",
          icon: "error",
        });
      }
    })
    .finally(() => {
      uni.hideLoading();
    });
};

const getCode = () => {
  if (isTiming.value) return;
  isTiming.value = true;
  second.value = 60;
  const timer = setInterval(() => {
    second.value--;
    if (second.value <= 0) {
      clearInterval(timer);
      isTiming.value = false;
    }
  }, 1000);

  request({
    url: "login/smsCode",
    method: "POST",
    data: { mobile: phone.value },
  }).then((res) => {
    uni.showToast({
      title: "验证码已发送",
      icon: "success",
    });
  });
};
</script>

<style scoped>
.page {
  line-height: 1.6;
  height: 100vh;
  padding: 10px;
  overflow: hidden;
  background: linear-gradient(135deg, #f5f7fa, #e4e7f1);

  display: flex;
  justify-content: center;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  padding: 40px 30px;
  width: 100%;
  max-width: 400px;
  transition: all 0.3s ease;
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo h1 {
  color: #333;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
}

.logo p {
  color: #666;
  font-size: 14px;
}

.input-group {
  margin-bottom: 20px;
}

.input-group label {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-size: 14px;
  font-weight: 500;
}

.input-with-button {
  display: flex;
}

.input {
  flex: 1;
  border: 1px solid #ddd;
  border-radius: 10px 0 0 10px;
  padding: 12px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s;
}

.input-with-button .input:hover {
  border-color: #6a11cb;
}

.input-with-button .button {
  line-height: 3;
  background: #f0f0f0;
  border: 1px solid #ddd;
  border-left: none;
  border-radius: 0 10px 10px 0;
  padding: 0 20px;
  cursor: pointer;
  font-size: 14px;
  color: #6a11cb;
  transition: all 0.3s;
  white-space: nowrap;
}

.input-with-button .button:hover {
  background: #e8e8e8;
}

.input-with-button .button:disabled {
  background: #f5f5f5;
  color: #999;
  cursor: not-allowed;
}

.input-field {
  width: 100%;
  border: 1px solid #ddd;
  border-radius: 10px;
  padding: 12px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s;
}

.input-field:hover {
  border-color: #6a11cb;
}

.login-button {
  width: 100%;
  background: linear-gradient(to right, #6a11cb, #2575fc);
  color: white;
  border: none;
  border-radius: 10px;
  padding: 15px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 10px;
  transition: transform 0.2s, box-shadow 0.2s;
  text-align: center;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(106, 17, 203, 0.4);
}

.login-button:active {
  transform: translateY(0);
}

.divider {
  display: flex;
  align-items: center;
  margin: 25px 0;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  border-bottom: 1px solid #ddd;
}

.divider span {
  padding: 0 15px;
  color: #999;
  font-size: 14px;
}

.other-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 20px;
}

.other-login button {
  background: white;
  border: 1px solid #ddd;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
}

.other-login button:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
}

.other-login i {
  font-size: 20px;
  color: #666;
}

.footer {
  text-align: center;
  margin-top: 25px;
  font-size: 12px;
  color: #999;
}

.footer a {
  color: #6a11cb;
  text-decoration: none;
}

@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }

  .logo h1 {
    font-size: 24px;
  }
}
</style>
